package cn.edu.hstc.wm.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.serialization.SoapObject;

import com.baidu.platform.comapi.map.a.o;

import android.util.Log;

import cn.edu.hstc.wm.AppConfigs;
import cn.edu.hstc.wm.Constants;
import cn.edu.hstc.wm.bean.User;
import cn.edu.hstc.wm.https.HttpUtils;
import cn.edu.hstc.wm.utils.LogUtil;
import cn.edu.hstc.wm.utils.MD5;

/**
 * @ClassName: UserService
 * @Description: 用户工具类,包括登录,注销等
 * 
 * @ModificationHistory
 */
public class UserService {

	/**
	 * 校验用户是否存在
	 * 
	 * @param name
	 * @param password
	 * @return 账号密码正确返回true
	 */
	public static boolean verifyUser(User user) {
		String soapAction = "login";
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("name", user.getName()));
		params.add(new BasicNameValuePair("password", MD5.md5(user.getPassword())));

		try {
			SoapObject resultObject = SoapService.getSoapObject(soapAction, params);
			
			LogUtil.d("verifyUser", "user = " + user.toString() + "  " + "resultObject = " + resultObject.toString());
			
			return Integer.parseInt(resultObject.getProperty(0).toString()) == 1 ? true : false;
			
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 根据用户名取得用户所有信息
	 * 
	 * @param name
	 * @return
	 */
	public static User login(User user) {
		String soapAction = "getUserByName_Pwd";
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("name", user.getName()));
		params.add(new BasicNameValuePair("password",MD5.md5(user.getPassword())));

		SoapObject resultObject = null;
		try {
			resultObject = SoapService.getSoapObject(soapAction, params);
			LogUtil.d("login  resultObject", "resultObject = "+ resultObject.toString());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

		try {
			return parseUser((SoapObject) resultObject.getProperty(0));
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	private static User parseUser(SoapObject resultObject) throws Exception{
		User user = new User();
		user.setName(resultObject.getPropertyAsString("name"));
		//user.setPassword(resultObject.getPropertyAsString("password"));
		user.setFace(resultObject.getPropertyAsString("imageName"));
		user.setJointime(resultObject.getPropertyAsString("registerTime"));
		user.setGender(resultObject.getPropertyAsString("sex"));
		
		//user.setLatestonline(resultObject.getPropertyAsString("lastTime"));
		return user;

	}

	// ///////////////////////////////////////Json 格式
	// ////////////////////////////////////////
/*
	*//**
	 * 用户登录
	 * 
	 * @param user
	 * @return
	 *//*
	public static User login(User user) {
		String resultContent = "";
		JSONObject jsonObject;
		Map<String, String> map = new HashMap<String, String>();
		map.put(Constants.HTTP_PARAMS_USER_NANE, user.getName());
		map.put(Constants.HTTP_PARAMS_USER_PW, user.getPassword());

		try {
			resultContent = HttpUtils.postRequest(AppConfigs.USER_LOGIN_URL, map);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		try {
			jsonObject = new JSONObject(resultContent);
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}

		return json2User(jsonObject);
	}

	*//**
	 * 格式化User->Json
	 * 
	 * @param user
	 * @return
	 *//*
	public static JSONObject user2JsonObject(User user) {

		return null;
	}

	*//**
	 * 将Json获取User
	 * 
	 * @param jsonObject
	 * @return
	 *//*
	public static User json2User(JSONObject jsonObject) {
		User user = new User();
		try {
			System.out.println(jsonObject.toString());
			user.setUid(jsonObject.getInt(Constants.JSON_KEY_USER_UID));
			user.setName(jsonObject.getString(Constants.JSON_KEY_USER_NAME));
			user.setPassword(jsonObject.getString(Constants.JSON_KEY_USER_PW));
			user.setGender(jsonObject.getString(Constants.JSON_KEY_USER_GENDER));
			user.setRememberMe(jsonObject.getBoolean(Constants.JSON_KEY_USER_REMEMBER_ME));
			user.setFace(jsonObject.getString(Constants.JSON_KEY_USER_FACE));
			// user.setJointime(jsonObject.getString(Constants.JSON_KEY_USER_JOIN_TIME));
			// 添加其他参数

		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
		return user;
	}
*/
	
	
	//////////////////////////////Sina login///////////////////////////////////
	/**
	 * 登录认证，如果以前授权使用过则返回TRUE，第一次使用授权返回fasle，并且注册
	 * @param uid
	 * @param access_token
	 * @return
	 */
	public static boolean loginAuth(String uid,String access_token){
		String soapAction = "loginAuth";
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("uid", uid));
		params.add(new BasicNameValuePair("access_token", access_token));
		
		try {
			SoapObject result = SoapService.getSoapObject(soapAction, params);
			Log.d("result object"	, " sina " + uid + "   " + access_token + "   " + result.toString());
			return Integer.parseInt(result.getPropertyAsString(0))==1?true:false;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * 授權用戶註冊
	 * @param uid
	 * @param access_token
	 */
	public static void authorize(String uid,String access_token){
		String soapAction = "authorize";
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("uid", uid));
		params.add(new BasicNameValuePair("access_token", access_token));
		
		SoapObject result = null;
		
		try {
			result = SoapService.getSoapObject(soapAction, params);
			Log.d("authorize", "" +result.toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public static User getUserByUid_Token(String uid,String access_token){
		String soapAction = "getUserByUid_Token";
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("uid", uid));
		params.add(new BasicNameValuePair("access_token", access_token));
		SoapObject result = null;
		
		try {
			result = SoapService.getSoapObject(soapAction, params);
			Log.d("getUserByUid_Token", "" + result.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			return parseSinaUser((SoapObject) result.getProperty(0));
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	private static User parseSinaUser(SoapObject object){
		User user = new User();
		user.setName("");
		//user.setPassword(resultObject.getPropertyAsString("password"));
		user.setFace(object.getPropertyAsString("imageName"));
		//user.setJointime(object.getPropertyAsString("registerTime"));
		user.setGender(object.getPropertyAsString("sex"));
		//user.setUid(object.getPropertyAsString("uid"));
		
		//user.setLatestonline(resultObject.getPropertyAsString("lastTime"));
		return user;
	}
}
